﻿using NCP.Recruiting.ApplicationServices.Interfaces;
using NCP.Recruiting.ApplicationServices.Messaging.Bookmarks;
using NCP.Recruiting.Web.Framework.Controllers;
using NCP.Recruiting.WebApp.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace NCP.Recruiting.WebApp.Controllers
{
    public class BookmarkController : AuthorizedController
    {
        #region Const
        private readonly ICompanyAppService _CompanyAppService;
        private readonly IBookmarkAppService _BookmarkAppService;
        private const int _pageSize = 15;
        private const String _dateFormat = "yyyy/MM/dd";
        private const String _dateFormatTime = "yyyy/MM/dd HH:mm:ss";
        private const String _defaultKeyword = "关键字过滤";
        #endregion

        public BookmarkController(ICompanyAppService companyAppService, IBookmarkAppService bookmarkAppService)
            : base(companyAppService)
        {
            _CompanyAppService = companyAppService;
            _BookmarkAppService = bookmarkAppService;
        }

        #region List
        public ActionResult List(PagingFilteringModel command)
        {
            if (command == null)
            {
                command = TempData["command"] as PagingFilteringModel;
            }

            //初始化分页组件
            if (command.PageSize <= 0)
                command.PageSize = _pageSize;
            if (command.PageNumber <= 0)
                command.PageNumber = 1;

            var response = _BookmarkAppService.GetBookmarks(new GetBookmarksRequest()
            {
                PageIndex = command.PageIndex,
                PageSize = command.PageSize,
                CompanyId = this.CurrentUserId
            });

            var model = new BookmarkListViewModel()
            {
                PagingFilteringContext = new PagingFilteringModel(),
                BookmarkList = response.ListItemModelList
            };
            model.PagingFilteringContext.LoadPagedList(response.ListItemModelList);

            TempData["command"] = command;
            return View(model);
        }

        [HttpPost]
        [ActionName("List")]
        [FormValueRequired("Delete")]
        public ActionResult Delete(Guid id)
        {
            _BookmarkAppService.Delete(new DeleteBookmarkRequest() { Id = id });
            return List(null);
        }

        [HttpPost]
        [ActionName("List")]
        [FormValueRequired("DeleteSelected")]
        public ActionResult DeleteSelected(String idList)
        {
            var list = (idList ?? String.Empty).Split(',').Where(x => !String.IsNullOrWhiteSpace(x)).Select(x => Guid.Parse(x)).ToList();
            list.ForEach(x => _BookmarkAppService.Delete(new DeleteBookmarkRequest() { Id = x }));
            return List(null);
        }

        [HttpPost]
        [ActionName("List")]
        [FormValueRequired("PreviewSelected")]
        public ActionResult PreviewSelected(String idList)
        {
            var list = (idList ?? String.Empty).Split(',').Where(x => !String.IsNullOrWhiteSpace(x)).Select(x => Guid.Parse(x)).ToList();
            TempData["PreviewIdList"] = list;
            return RedirectToAction("Priview", "Resume", new { id = list.First(), isShowSelected = true });
        }
        #endregion
    }
}
